package cn.darkhorse.admin.controller.basic;

import cn.darkhorse.basic.domain.QmsHviMachine;
import cn.darkhorse.basic.service.IQmsHviMachineService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Hvi机台管理Controller
 *
 * @author ytq
 * @date 2024-07-18
 */
@RestController
@RequestMapping("/basic/hviMachine")
public class QmsHviMachineController extends BaseController
{
    @Resource
    private IQmsHviMachineService QmsHviMachineService;

    /**
     * 查询hvi机台管理列表
     */
@ApiOperation("查询hvi机台管理列表")
    @GetMapping("/list")
    public TableDataInfo list(QmsHviMachine qmsHviMachine)
    {
        startPage();
        List<QmsHviMachine> list = QmsHviMachineService.selectQmsHviMachineList(qmsHviMachine);
        return getDataTable(list);
    }


    /**
     * 获取hvi机台管理详细信息
     */
    @ApiOperation("获取hvi机台管理详细信息")
    @GetMapping(value = "/view")
    public AjaxResult view( Long id)
    {
        return success(QmsHviMachineService.selectQmsHviMachineById(id));
    }

    /**
     * 新增hvi机台管理
     */
    @ApiOperation("新增hvi机台管理")
    @Log(title = "hvi机台管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "save")
    public AjaxResult add(@RequestBody QmsHviMachine qmsHviMachine)
    {

        return toAjax(QmsHviMachineService.saveQmsHviMachine(qmsHviMachine));
    }

    /**
     * 删除hvi机台管理
     */
    @ApiOperation("删除hvi机台管理")
    @Log(title = "hvi机台管理", businessType = BusinessType.DELETE)
	@PostMapping("/delete")
    public AjaxResult delete(@RequestBody Long id)
    {
        return toAjax(QmsHviMachineService.deleteQmsHviMachineById(id));
    }


    /**
     * 批量删除hvi机台管理
     */
            @ApiOperation("批量删除hvi机台管理")
    @Log(title = "hvi机台管理", businessType = BusinessType.DELETE)
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(@RequestBody Long[] ids)
    {
        return toAjax(QmsHviMachineService.deleteQmsHviMachineByIds(ids));
    }

    /**
     * 导出hvi机台管理列表
     */
            @ApiOperation("导出hvi机台管理列表")
    @Log(title = "hvi机台管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsHviMachine qmsHviMachine)
    {
        List<QmsHviMachine> list = QmsHviMachineService.selectQmsHviMachineList(qmsHviMachine);
        ExcelUtil<QmsHviMachine> util = new ExcelUtil<QmsHviMachine>(QmsHviMachine.class);
        util.exportExcel(response, list, "hvi机台管理数据");
    }

    /**
     * 导入hvi机台管理列表
     */
    @ApiOperation("导入hvi机台管理列表")
    @Log(title = "hvi机台管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<QmsHviMachine> util = new ExcelUtil<QmsHviMachine>(QmsHviMachine.class);
        List<QmsHviMachine> qmsHviMachineList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();

        if (StringUtils.isNull(qmsHviMachineList) || qmsHviMachineList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (QmsHviMachine qmsHviMachine : qmsHviMachineList)
        {
            try
            {
                int res = QmsHviMachineService.saveQmsHviMachine(qmsHviMachine);
                if (res>0){
                    successNum++;
                }
            }
            catch (Exception e)
            {
                failureNum++;
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return success(successMsg);
    }

    /**
     * 下载模板
     */
    @ApiOperation("下载模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<QmsHviMachine> util = new ExcelUtil<QmsHviMachine>(QmsHviMachine.class);
        util.importTemplateExcel(response, "hvi机台管理数据");
    }
}
